package com.shushan.base;

import android.app.Activity;
import android.app.Dialog;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.fingerprintidentify.FingerprintIdentify;
import com.android.fingerprintidentify.base.BaseFingerprint;
import com.android.log.LogActivity;
import com.android.net.bean.NetRetBean;
import com.android.net.util.NetToastUtil;
import com.shushan.home.TabHomeActivity;
import com.shushan.login.LoginOrRegisterActivity;
import com.shushan.service.DialRTAService;
import com.shushan.service.HomeFrequeRTAService;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.BuildConfig;
import com.shushan.shushanbao.R;
import com.shushan.ui.PopupDialog;
import com.shushan.ui.VerificationCodeView;
import com.shushan.ui.WaitDialog;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.StringTools;
import com.shushan.util.SystemUtils;
import com.shushan.util.ToastUtils;
import com.shushan.util.UmengEventUtil;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;

import java.util.Timer;
import java.util.TimerTask;


/**
 * @ClassName: BaseActivity
 * @Desciption: 所有的Activity的基类
 * @author: yichaohua
 * @date: 2017-12-29
 */
public class BaseActivity extends AppCompatActivity {

    private static final String TAG = "BaseActivity";

    private WaitDialog mWaitDialog;
    private Dialog singleLoginDialog;
    private Dialog parentPswDialog;
    private VerificationCodeView veriCodeView;
    private boolean isAllowBack = false;
    protected boolean isForegroud = false;
    private long currentTimeMillis = 0;
    private KeyHomeReceiver keyHomeReceiver;
    private FingerprintIdentify mFingerprintIdentify;

    private static final int MAX_AVAILABLE_TIMES = 5;

    private String [] noHomeActivitys = {"SplashActivity","VideoPageActivity","GuideActivity",
            "LoginOrRegisterActivity","VerificationCodeActivity","ProtocolWebActivity"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PushAgent.getInstance(this).onAppStart();
        ActivityStackManager.getActivityStackManager().pushActivity(this);
        mWaitDialog = new WaitDialog(this, R.style.Dialog_Translucent_Background);
    }

    @Override
    protected void onStart() {
        super.onStart();
        bindBack();
        keyHomeReceiver = new KeyHomeReceiver();
        registerReceiver(keyHomeReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

    }

    @Override
    protected void onResume() {
        super.onResume();
        UmengEventUtil.onResume(this); // 基础指标统计，不能遗漏
        isForegroud = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        UmengEventUtil.onPause(this); // 基础指标统计，不能遗漏
        isForegroud = false;
        boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
        TelephonyManager telMgr = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);
        if(telMgr.getCallState() == TelephonyManager.CALL_STATE_RINGING
                || telMgr.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK){
            Log.i(TAG,"onPause() phone is ringing isChildControl=" + isChildControl);
            if(isChildControl){
                Intent intent = new Intent(this,DialRTAService.class);
                intent.putExtra("rtaType",3);
                intent.putExtra("allowPackName",Constants.ALLOW_DIAL_PACK_NAME);
                startService(intent);
            }
        }else {
            Log.i(TAG,"onPause() isChildControl=" + isChildControl + " isAllowBack=" + isAllowBack);
            if(!isAllowBack && isChildControl){
                    SystemUtils.sendForegActivity(this,TabHomeActivity.class);

                    if(System.currentTimeMillis() - currentTimeMillis <= 1000){
                        startService(new Intent(this,HomeFrequeRTAService.class));
                    }
            }
        }
        isAllowBack = false;
        currentTimeMillis = System.currentTimeMillis();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(keyHomeReceiver != null){
            unregisterReceiver(keyHomeReceiver);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityStackManager.getActivityStackManager().popActivity(this);
        if(mWaitDialog != null  && !isFinishing()){
            mWaitDialog.dismiss();
            mWaitDialog = null;
        }

        if(singleLoginDialog != null  && !isFinishing()){
            singleLoginDialog.dismiss();
            singleLoginDialog = null;
        }

        if(parentPswDialog != null  && !isFinishing()){
            parentPswDialog.dismiss();
            parentPswDialog = null;
        }

        if(mFingerprintIdentify != null){
            mFingerprintIdentify.cancelIdentify();
        }
    }

    public void startActivityAllow(Intent intent) {
        if(intent == null){
            return;
        }

        isAllowBack = true;
        startActivity(intent);
    }

    public void startActivityForResultAllow(Intent intent) {
        if(intent == null){
            return;
        }

        isAllowBack = true;
        startActivityForResult(intent, 100);
    }

    public void startActivityForResultAllow(Intent intent,int requestCode) {
        if(intent == null){
            return;
        }

        isAllowBack = true;
        startActivityForResult(intent, requestCode);
    }

    public void startActivity(Class<? extends Activity> cls) {
        isAllowBack = true;
        startActivity(new Intent(this, cls));
    }

    protected void startActivityForResult(Class<? extends Activity> cls) {
        isAllowBack = true;
        startActivityForResult(new Intent(this, cls), 100);
    }

    /**
     * 跳转Activity
     * skip Another Activity
     *
     * @param cls
     */
    protected void skipAnotherActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
        onBackAction();
    }

    private void bindBack() {
        ImageView topLeftImg = findViewById(R.id.top_left_back_img);
        if (topLeftImg != null) {
            topLeftImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onTitleBack();
                }
            });
        }
    }

    protected void onTitleBack() {
        onBackAction();
    }

    /**
     * 退出应用
     * called while exit app.
     */
    public void exitLogic() {
        isAllowBack = true;
        ActivityStackManager.getActivityStackManager().popAllActivity();//remove all activity.
        System.exit(0);//system exit.
    }

    @Override
    public void setTitle(int titleId) {
        TextView tvTitle = findViewById(R.id.top_title);
        if(tvTitle != null){
            tvTitle.setText(titleId);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        TextView tvTitle = findViewById(R.id.top_title);
        if(tvTitle != null){
            tvTitle.setText(title);
        }
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (null != this.getCurrentFocus()) {
            //点击空白位置 隐藏软键盘
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }

    protected void onBackAction() {
        if(mWaitDialog != null){
            mWaitDialog.dismiss();
        }

        if(singleLoginDialog != null){
            singleLoginDialog.dismiss();
        }

        if(parentPswDialog != null){
            parentPswDialog.dismiss();
        }

        isAllowBack = true;
        finish();
    }

    protected void onBackAction(int resultCode) {
        if(mWaitDialog != null){
            mWaitDialog.dismiss();
        }

        if(singleLoginDialog != null){
            singleLoginDialog.dismiss();
        }

        if(parentPswDialog != null){
            parentPswDialog.dismiss();
        }

        isAllowBack = true;
        setResult(resultCode);
        finish();
    }

    protected void onBackAction(int resultCode,Intent intent) {
        if(mWaitDialog != null){
            mWaitDialog.dismiss();
        }

        if(singleLoginDialog != null){
            singleLoginDialog.dismiss();
        }

        if(parentPswDialog != null){
            parentPswDialog.dismiss();
        }

        isAllowBack = true;
        setResult(resultCode,intent);
        finish();
    }

    protected void showToast(String text) {
        ToastUtils.showToast(this, text);
    }

    public void showNetError(NetRetBean netRetBean) {
        Log.e(TAG, "netRetBean=" + netRetBean.toString());
        if(netRetBean.getServerCode() == 304){
            boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
            if(!isChildControl || TextUtils.isEmpty(SharedPreferencesUtils.getStringDate(Constants.PARENT_PASSWD_SHAREPREFERS_KEY))) {
                showHasLoginDialogNoPsw();
            }else{
                showHasLoginDialog();
            }
            return;
        }


        NetToastUtil.requestError(this, netRetBean);
    }

    protected void showNoMsgError(NetRetBean netRetBean) {
        Log.e(TAG, "netRetBean=" + netRetBean.toString());
        if(netRetBean.getServerCode() == 304){
            boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
            if(!isChildControl || TextUtils.isEmpty(SharedPreferencesUtils.getStringDate(Constants.PARENT_PASSWD_SHAREPREFERS_KEY))) {
                showHasLoginDialogNoPsw();
            }else {
                showHasLoginDialog();
            }
        }
    }

    //隐藏状态栏
    public static void hideStatusBar(Activity activity){
        Window window = activity.getWindow();
        //隐藏标题栏
        //activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
    }

    private void showHasLoginDialogNoPsw() {
        final PushAgent mPushAgent = PushAgent.getInstance(BaseActivity.this);
        int currentChildId = BabyApplication.getInstance().getCurrentChildId();
        if(currentChildId > 0){
            mPushAgent.deleteAlias(currentChildId+"", Constants.UMENG_ALIAS_CHILD_ID_KEY, new UTrack.ICallBack() {
                @Override
                public void onMessage(boolean isSuccess, String message) {
                    Log.i(TAG,"isSuccess=" + isSuccess + " message=" + message);
                }
            });
        }

        int useId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
        if(useId > 0){
            mPushAgent.deleteAlias(useId+"", Constants.UMENG_ALIAS_USE_ID_KEY, new UTrack.ICallBack() {
                @Override
                public void onMessage(boolean isSuccess, String message) {
                    Log.i(TAG,"isSuccess=" + isSuccess + " message=" + message);
                }
            });
        }

        final PopupDialog popupDialog = new PopupDialog(this);
        popupDialog.setMessage("发现您的账号：" + SharedPreferencesUtils.getStringDate(
                Constants.LOGIN_MOBILE_SHAREPREFERS_KEY) + "已经在别的设备上登录，是否重新登录？");
        popupDialog.setPositiveButton("去登录", new PopupDialog.DialogOnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(LoginOrRegisterActivity.class);
                onBackAction();
                SharedPreferencesUtils.setBooleanDate(Constants.IS_LOGIN_SHAREPREFERS_KEY,false);
            }
        });
        popupDialog.setNegativeButton("取消", new PopupDialog.DialogOnClickListener() {
            @Override
            public void onClick(View v) {
                popupDialog.dismiss();
            }
        });

        if (!isFinishing()) {
            popupDialog.show(getWindow().getDecorView(), ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
        }
    }

    private void showHasLoginDialog(){
        final PushAgent mPushAgent = PushAgent.getInstance(BaseActivity.this);
        int currentChildId = BabyApplication.getInstance().getCurrentChildId();
        if(currentChildId > 0){
            mPushAgent.deleteAlias(currentChildId+"", Constants.UMENG_ALIAS_CHILD_ID_KEY, new UTrack.ICallBack() {
                @Override
                public void onMessage(boolean isSuccess, String message) {
                    Log.i(TAG,"isSuccess=" + isSuccess + " message=" + message);
                }
            });
        }

        int useId = SharedPreferencesUtils.getIntDate(Constants.CURRENT_USE_ID_SHAREPREFERS_KEY);
        if(useId > 0){
            mPushAgent.deleteAlias(useId+"", Constants.UMENG_ALIAS_USE_ID_KEY, new UTrack.ICallBack() {
                @Override
                public void onMessage(boolean isSuccess, String message) {
                    Log.i(TAG,"isSuccess=" + isSuccess + " message=" + message);
                }
            });
        }
        VerificationCodeView codeView = null;
        if(singleLoginDialog == null){
            singleLoginDialog = new Dialog(this,R.style.Dialog_Translucent_Background);
            singleLoginDialog.setContentView(R.layout.dialog_single_login);
            singleLoginDialog.setCancelable(false);
            Button btnCancel = singleLoginDialog.findViewById(R.id.btn_cancel);
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    singleLoginDialog.dismiss();
                    if(mFingerprintIdentify != null){
                        mFingerprintIdentify.cancelIdentify();
                    }
                }
            });
            codeView = singleLoginDialog.findViewById(R.id.verification_code_view);
            codeView.setOnCodeFinishListener(new VerificationCodeView.OnCodeFinishListener() {
                @Override
                public void onComplete(String content) {
                    if(StringTools.pswValide(content)) {
                        startActivity(LoginOrRegisterActivity.class);
                        onBackAction();
                        SharedPreferencesUtils.setBooleanDate(Constants.IS_LOGIN_SHAREPREFERS_KEY,false);
                    }else{
                        showToast("家长密码错误，请重新输入");
                    }
                }
            });
        }
        if(mFingerprintIdentify == null){
            mFingerprintIdentify = new FingerprintIdentify(BabyApplication.getInstance(), new BaseFingerprint.FingerprintIdentifyExceptionListener() {
                @Override
                public void onCatchException(Throwable exception) {
                    Log.e(TAG,exception.getLocalizedMessage());
                }
            });
        }
        TextView tvDes = singleLoginDialog.findViewById(R.id.tv_description);
        ImageView parentImg = singleLoginDialog.findViewById(R.id.parent_psw_img);
        ImageView fingerIconImg = singleLoginDialog.findViewById(R.id.parent_finger_icon_img);
        final TextView hintText = singleLoginDialog.findViewById(R.id.parent_dialog_hint_tv);
        hintText.setVisibility(View.GONE);
        if(mFingerprintIdentify.isFingerprintEnable()
                && mFingerprintIdentify.isHardwareEnable()
                && mFingerprintIdentify.isRegisteredFingerprint()){

            parentImg.setVisibility(View.GONE);
            fingerIconImg.setVisibility(View.VISIBLE);

            tvDes.setText("发现您的账号：" + SharedPreferencesUtils.getStringDate(
                    Constants.LOGIN_MOBILE_SHAREPREFERS_KEY) + " 已经在别的设备上登录，请验证指纹或者家长密码后重新登陆。");

            mFingerprintIdentify.startIdentify(5, new BaseFingerprint.FingerprintIdentifyListener() {
                @Override
                public void onSucceed() {
                    startActivity(LoginOrRegisterActivity.class);
                    onBackAction();
                    SharedPreferencesUtils.setBooleanDate(Constants.IS_LOGIN_SHAREPREFERS_KEY,false);
                    singleLoginDialog.dismiss();
                }

                @Override
                public void onNotMatch(int availableTimes) {
                    hintText.setVisibility(View.VISIBLE);
                    hintText.setText("指纹不匹配，还剩余" + availableTimes + "次机会");
                    Animation shake = AnimationUtils.loadAnimation(BaseActivity.this, R.anim.shake);
                    hintText.startAnimation(shake);

                }

                @Override
                public void onFailed(boolean isDeviceLocked) {
                    hintText.setText("指纹尝试过多，请使用家长密码");
                    Animation shake = AnimationUtils.loadAnimation(BaseActivity.this, R.anim.shake);
                    hintText.startAnimation(shake);
                }

                @Override
                public void onStartFailedByDeviceLocked() {
                    hintText.setText("指纹尝试过多，请使用家长密码");
                    Animation shake = AnimationUtils.loadAnimation(BaseActivity.this, R.anim.shake);
                    hintText.startAnimation(shake);
                }
            });
        }else {
            parentImg.setVisibility(View.VISIBLE);
            fingerIconImg.setVisibility(View.GONE);
            tvDes.setText("发现您的账号：" + SharedPreferencesUtils.getStringDate(
                    Constants.LOGIN_MOBILE_SHAREPREFERS_KEY) + " 已经在别的设备上登录，请输入家长密码后重新登陆。");
        }

        if(codeView != null){
            codeView.clear();
        }

        if(!singleLoginDialog.isShowing() && !isFinishing()){
            singleLoginDialog.show();
        }
    }

    private void showParentPswDialog() {
        if(parentPswDialog == null){
            parentPswDialog = new Dialog(this,R.style.Dialog_Translucent_Background);
            parentPswDialog.setContentView(R.layout.change_to_parent_dialog);
            parentPswDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

            veriCodeView = parentPswDialog.findViewById(R.id.verification_code_view);
            veriCodeView.setOnCodeFinishListener(new VerificationCodeView.OnCodeFinishListener() {
                @Override
                public void onComplete(String content) {
                    if(StringTools.pswValide(content)){
                        isAllowBack = true;
                        ActivityStackManager.getActivityStackManager().popAllActivity();
                    }else {
                        showToast("家长密码错误");
                        if(veriCodeView != null){
                            veriCodeView.clear();
                        }
                    }
                }
            });
            Button coloseButton = parentPswDialog.findViewById(R.id.btn_cancel);
            coloseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    parentPswDialog.dismiss();
                    hintKeyBoard();

                    if(mFingerprintIdentify != null){
                        mFingerprintIdentify.cancelIdentify();
                    }
                }
            });

            parentPswDialog.setCancelable(false);
        }

        if(mFingerprintIdentify == null){
            mFingerprintIdentify = new FingerprintIdentify(BabyApplication.getInstance(), new BaseFingerprint.FingerprintIdentifyExceptionListener() {
                @Override
                public void onCatchException(Throwable exception) {
                    Log.e(TAG,exception.getLocalizedMessage());
                }
            });
        }
        ImageView parentImg = parentPswDialog.findViewById(R.id.parent_psw_img);
        ImageView fingerIconImg = parentPswDialog.findViewById(R.id.parent_finger_icon_img);
        final TextView hintText = parentPswDialog.findViewById(R.id.parent_dialog_hint_tv);
        hintText.setVisibility(View.GONE);
        if(mFingerprintIdentify.isFingerprintEnable()
                && mFingerprintIdentify.isHardwareEnable()
                && mFingerprintIdentify.isRegisteredFingerprint()){

            parentImg.setVisibility(View.GONE);
            fingerIconImg.setVisibility(View.VISIBLE);

            ((TextView)parentPswDialog.findViewById(R.id.parent_dialog_tv)).setText("请验证指纹或者家长密码退出");

            mFingerprintIdentify.startIdentify(MAX_AVAILABLE_TIMES, new BaseFingerprint.FingerprintIdentifyListener() {
                @Override
                public void onSucceed() {
                    isAllowBack = true;
                    ActivityStackManager.getActivityStackManager().popAllActivity();
                }

                @Override
                public void onNotMatch(int availableTimes) {
                    hintText.setVisibility(View.VISIBLE);
                    hintText.setText("指纹不匹配，还剩余" + availableTimes + "次机会");
                    Animation shake = AnimationUtils.loadAnimation(BaseActivity.this, R.anim.shake);
                    hintText.startAnimation(shake);

                }

                @Override
                public void onFailed(boolean isDeviceLocked) {
                    hintText.setText("指纹尝试过多，请使用家长密码");
                    Animation shake = AnimationUtils.loadAnimation(BaseActivity.this, R.anim.shake);
                    hintText.startAnimation(shake);
                }

                @Override
                public void onStartFailedByDeviceLocked() {
                    hintText.setText("指纹尝试过多，请使用家长密码");
                    Animation shake = AnimationUtils.loadAnimation(BaseActivity.this, R.anim.shake);
                    hintText.startAnimation(shake);
                }
            });
        }else {
            parentImg.setVisibility(View.VISIBLE);
            fingerIconImg.setVisibility(View.GONE);
            ((TextView)parentPswDialog.findViewById(R.id.parent_dialog_tv)).setText("请输入家长密码退出");
        }

        if(parentPswDialog != null && !parentPswDialog.isShowing() && !isFinishing()){
            parentPswDialog.show();
        }
    }

    /**
     * 显示加载动画
     */
    public void showWaitDialog() {
        if(mWaitDialog != null && !mWaitDialog.isShowing() && !isFinishing()){
            mWaitDialog.show();
        }
    }

    /**
     * 关闭加载动画
     *
     */
    public void dismissWaitDialog() {
        if (isFinishing()){
            return;
        }

        if(mWaitDialog != null && mWaitDialog.isShowing()){
            mWaitDialog.dismiss();
        }
    }

    public boolean isAllowBack() {
        return isAllowBack;
    }

    public void setAllowBack(boolean allowBack) {
        isAllowBack = allowBack;
    }

    private class KeyHomeReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String reason = intent.getStringExtra(Constants.SYS_KEY);
            Log.i(TAG,"reason=" + reason);

            boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
            if(!TextUtils.isEmpty(reason) && reason.equals(Constants.SYS_HOME_KEY) && allowHome() && isChildControl){
                Log.i(TAG,"KeyHomeReceiver homekey");

                SystemUtils.sendForegActivity(context,TabHomeActivity.class);

//                ToastUtils.showToast(BaseActivity.this,"管控模式下不允许退出");
                String subClassName = getBaseClass();
                if(subClassName.equals("TabHomeActivity")){
                    onQuitDialogShow();
                    showParentPswDialog();
                }
            }
        }
    }

    private boolean allowHome() {
        String subClassName = getClass().getSimpleName();
        if(subClassName.equals("TabHomeActivity")){
            return !isAllowBack;
        }

        for (String className : noHomeActivitys){
            if(className.equals(subClassName)){
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        String subClassName = getClass().getSimpleName();
        Log.i(TAG,"dispatchKeyEvent subClassName=" + subClassName);
        boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
        if(isChildControl && subClassName.equals("TabHomeActivity") && event.getKeyCode() == KeyEvent.KEYCODE_BACK){
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

        @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            if (BuildConfig.DEBUG) {
                AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                int music = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                int volume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
                if (volume == 0 || music == 0) {
                    isAllowBack = true;
                    startActivity(LogActivity.class);
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }




    private String getBaseClass(){
        return  getClass().getSimpleName();
    }

    public void onQuitDialogShow(){

    }

    public void hintKeyBoard() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager manager = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
                View view = getCurrentFocus();
                if(manager != null && view != null){
                    manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        }, 10);
    }
}

